package gameFight;

/**
 * 作者:TC
 * 时间:玩家奖励类 20:36
 * 描述:没有bug
 */
import java.util.Random;
public class  reward {
    private double firstRewardProbability;
    private double secondRewardProbability;

    private int exp;

    //第一种和第二种奖励的可能性构造方法
    public reward(double firstRewardProbability, double secondRewardProbability) {
        this.firstRewardProbability = firstRewardProbability;
        this.secondRewardProbability = secondRewardProbability;
        //初始化exp=0奖励的经验值
        this.exp=0;
    }

    //随机选取奖励一和奖励二给角色
    public String giveReward() {
        Random random = new Random();
        double randomNumber = random.nextDouble();

        if (randomNumber < firstRewardProbability) {
            return selectItemFromFirstReward();
        } else {
            return selectItemFromSecondReward();
        }
    }

    //随机抽取一个奖励返回给角色
    private String selectItemFromReward(double[] probabilities, String[] items) {
        Random random = new Random();
        if (probabilities.length != items.length) {
            System.out.println("函数参数错误");
        }

        double[] cumulativeProbabilities = new double[probabilities.length];
        double cumulativeProbability = 0;
        for (int i = 0; i < probabilities.length; i++) {
            cumulativeProbability += probabilities[i];
            cumulativeProbabilities[i] = cumulativeProbability;
        }
//返回概率区间所处在的物品
        double r = random.nextDouble();
        for (int i = 0; i < cumulativeProbabilities.length; i++) {
            if (r <= cumulativeProbabilities[i]) {
                return items[i];
            }
        }
        return null;
    }

    //方案一的奖励机制
    public String selectItemFromFirstReward() {
        this.exp = 100; // 玩家获得100经验值
        double[] probabilities = {0.2, 0.2, 0.1, 0.2, 0.3};
        String[] items = {"金刚石", "红宝石", "乾坤袋", "如意丹", "金柳露"};
        return selectItemFromReward(probabilities, items);

    }

    //方案二的奖励机制
    public String selectItemFromSecondReward() {
        this.exp = 200; // 玩家获得200经验值
        double[] probabilities = {0.1, 0.1, 0.3, 0.2, 0.15, 0.15};
        String[] items = {"净瓶甘露", "人参果", "藏宝图", "黑宝石", "上古灵图", "避水珠"};
        return selectItemFromReward(probabilities, items);
    }
    //用一个getexp方法获取奖励的经验值
    public int getExp() {
        return exp;
    }
}
